package com.razorthink.shell.commands.essential;

import java.io.File;

import com.razorthink.shell.core.AbstractCommand;
import com.razorthink.shell.core.BatchExecutor;
import com.razorthink.shell.core.EnvironmentVariable;
import com.razorthink.shell.core.ParamParser;
import com.razorthink.shell.utils.CommandLogger;

public class Batch extends AbstractCommand {

	public Batch(String baseCommand, int params) {
		super(baseCommand, params);
	}

	@Override
	public void execute() {
		String file = super.getCommandParser().getParams().get(0);
		ParamParser p = new ParamParser(file, this);
		if (p.isEnvVar()) {
			EnvironmentVariable v = p.getEnvVar();
			if (v == null) {
				CommandLogger.logInfo("env var '" + p.getEnvVarString()
						+ "' not set");
				return;
			} else {
				file = (String) v.getValue();
			}
		}
		BatchExecutor bat = new BatchExecutor(new File(file),
				super.getCommandList(), this.getAppContext());
		bat.execute();
	}

	@Override
	public String getDescription() {
		return "execute batch file";
	}

	@Override
	public String getUsage() {
		return "batch [filename]";
	}

}
